package com.dream.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 匠人码农
 * @date 2020/11/08 19:42
 * 概要：
 *     Session会话
 *     1.创建
 *     2.获取
 *     3.判断是否为新创建的会话
 *     4.获取会话的唯一识别ID
 *     5.在session中存储数据
 *     6.从session中获取数据
 *     7.sessino的生命周期设定
 *       1> 默认的生命时常30分钟
 *       2> 个性化设定session的时常
 *       3> 设置session立即无效
 *
 *
 */

public class SessionServlet extends BaseServlet{
    /**
     * 1.创建session会话
     * 2.获取session会话
     * 3.判断会话是否为新创建
     * 4.获取session唯一识别ID
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void createOrGetSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取session
        HttpSession session = req.getSession();

        //判断是否为新创建
        boolean newFlg = session.isNew();

        //获取唯一识别ID
        String sessionId = session.getId();

        resp.getWriter().write("会话ID：" + sessionId + "<br/>" + "是否为新创建：" + newFlg + "<br/>");

    }

    /**
     * 5.在session里存储数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void setAttributeSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取session
        HttpSession session = req.getSession();

        //设置属性和值
        session.setAttribute("key1","value1");

        resp.getWriter().write("会话中设置属性[key1=value1]");

    }

    /**
     * 6.从sessino中获取数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getAttributeSession(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取session
        HttpSession session = req.getSession();

        //设置属性和值
        String val = (String)session.getAttribute("key1");

        resp.getWriter().write("会话中属性：key1=" + val);

    }

    /**
     * 7. 1> session默认时常
     *    默认时常是1800秒即30分钟
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取session
        HttpSession session = req.getSession();

        //获取会话的默认时间
        int defaultTime = session.getMaxInactiveInterval();

        resp.getWriter().write("会话默认时常是：" + defaultTime + "秒");

    }

    /**
     * 7. 2> 个性化设定session的时常
     *   将session时常设定为3s
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void life3s(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取session
        HttpSession session = req.getSession();

        //设置会话为3秒
        session.setMaxInactiveInterval(3);

        resp.getWriter().write("会话设定成3秒了！" + "<br/>是否是新创建:" + session.isNew());

    }


    /**
     * 7. 3> 设置立马失效
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取session
        HttpSession session = req.getSession();

        //设置会话为3秒
        session.invalidate();

        resp.getWriter().write("会话设置成立即销毁了！");

    }

}
